// 获取供应商 或招投标端token 设置请求头
import { getCommonStoragePrefix } from '@fl/utils/env'
import Cookie from 'js-cookie'

function getQueryParams() {
    const queryString = window.location.search.substring(1)
    const params = {}
    if (queryString) {
        const keyValuePairs = queryString.split('&')
        for (const pair of keyValuePairs) {
            const [key, value] = pair.split('=')
            if (key && value) {
                params[key] = decodeURIComponent(value)
            }
        }
    }
    return params
}

export function useInit() {
    const query: any = getQueryParams()
    const accessToken = `${getCommonStoragePrefix()}__access_token`

    let str = ''
    if (query?.token) {
        str = query.token.toString()
    }

    if (query?.source) {
        // 缓存来源 开通会员入参
        window.localStorage.setItem('MEMBER_SOURCE', query?.source)
    }

    if (str) {
        Cookie.set(accessToken, str)
        localStorage.setItem(accessToken, str)
    }
}
